package com.guigu.base.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guigu.base.entity.TransportLineType;
import com.guigu.base.entity.dto.TransportLineTypeDto;
import com.guigu.base.service.TransportLineTypeService;
import com.guigu.common.utils.PageResponse;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 线路类型表 前端控制器
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-08
 */
@RestController
@RequestMapping("/goods/pd-transport-line-type")
@Api(tags = "线路类型管理")
public class TransportLineTypeController {
    @Autowired
    private TransportLineTypeService transportLineTypeService;

    /**
     * 添加线路类型
     *
     * @param dto 线路类型信息
     * @return 线路类型信息
     */
    @PostMapping("")
    @ApiOperation("添加线路类型")
    public R<TransportLineTypeDto> saveTransportLineType(@RequestBody TransportLineTypeDto dto) {
        TransportLineType TransportLineType = new TransportLineType();
        BeanUtils.copyProperties(dto, TransportLineType);
        TransportLineType = transportLineTypeService.saveTransportLineType(TransportLineType);
        BeanUtils.copyProperties(TransportLineType, dto);
        return new R("2000", dto);
    }

    /**
     * 根据id获取线路类型详情
     *
     * @param id 线路类型id
     * @return 线路类型详情
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取线路类型详情")
    public R<TransportLineTypeDto> fineById(@PathVariable(name = "id") String id) {
        TransportLineType TransportLineType = transportLineTypeService.getById(id);
        TransportLineTypeDto dto = new TransportLineTypeDto();
        BeanUtils.copyProperties(TransportLineType, dto);
        return new R<>("2000", dto);
    }

    /**
     * 获取线路类型分页数据
     *
     * @param page       页码
     * @param pageSize   页尺寸
     * @param typeNumber 类型编号
     * @param name       类型名称
     * @param agencyType 机构类型
     * @return 线路类型分页数据
     */
    @GetMapping("/page")
    @ApiOperation("获取线路类型分页数据")
    public R<PageResponse<TransportLineTypeDto>> findByPage(@RequestParam(name = "page") Integer page,
                                                            @RequestParam(name = "pageSize") Integer pageSize,
                                                            @RequestParam(name = "typeNumber", required = false) String typeNumber,
                                                            @RequestParam(name = "name", required = false) String name,
                                                            @RequestParam(name = "agencyType", required = false) Integer agencyType) {
        IPage<TransportLineType> transportLineTypePage = transportLineTypeService.findByPage(page, pageSize, typeNumber, name, agencyType);
        List<TransportLineTypeDto> dtoList = new ArrayList<>();
        transportLineTypePage.getRecords().forEach(TransportLineType -> {
            TransportLineTypeDto dto = new TransportLineTypeDto();
            BeanUtils.copyProperties(TransportLineType, dto);
            dtoList.add(dto);
        });
        return new R<>("2000", PageResponse.<TransportLineTypeDto>builder().items(dtoList).pagesize(pageSize).page(page)
                .counts(transportLineTypePage.getTotal()).pages(transportLineTypePage.getPages()).build());
    }

    /**
     * 获取线路类型列表
     *
     * @param ids 线路类型id列表
     * @return 线路类型列表
     */
    @GetMapping("")
    @ApiOperation("获取线路类型列表")

    public R<List<TransportLineTypeDto>> findAll(@RequestParam(name = "ids", required = false) List<String> ids) {
        return new R<>("2000", transportLineTypeService.findAll(ids).stream().map(TransportLineType -> {
            TransportLineTypeDto dto = new TransportLineTypeDto();
            BeanUtils.copyProperties(TransportLineType, dto);
            return dto;
        }).collect(Collectors.toList()));
    }

    /**
     * 更新线路类型信息
     *
     * @param id  线路类型id
     * @param dto 线路类型信息
     * @return 线路类型信息
     */
    @PutMapping("/{id}")
    @ApiOperation("更新线路类型信息")

    public R<TransportLineTypeDto> update(@PathVariable(name = "id") String id, @RequestBody TransportLineTypeDto dto) {
        dto.setId(id);
        TransportLineType TransportLineType = new TransportLineType();
        BeanUtils.copyProperties(dto, TransportLineType);
        TransportLineType.setLastUpdateTime(new Date());
        transportLineTypeService.updateById(TransportLineType);
        return new R<>("2000", dto);
    }

    /**
     * 删除线路类型
     *
     * @param id 线路类型id
     * @return 返回信息
     */
    @PutMapping("/{id}/disable")
    @ApiOperation("删除线路类型")

    public R disable(@PathVariable(name = "id") String id) {
        transportLineTypeService.disableById(id);
        return new R(ResultCode.SUCCESS_CODE, "删除线路类型成功!", true);
    }
}

